package com.gxa.service.impl;

import com.gxa.common.JsonResult;
import com.gxa.common.PageRecv;
import com.gxa.common.R;
import com.gxa.mapper.DocumentMapper;
import com.gxa.mapper.EmployeeMapper;
import com.gxa.mapper.RepairOrderMapper;
import com.gxa.mapper.WorksheetMapper;
import com.gxa.pojo.Employee;
import com.gxa.pojo.RepairOrder;
import com.gxa.service.RepairOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class RepairOrderServiceImpl implements RepairOrderService {
    @Autowired
    private RepairOrderMapper repairOrderMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private WorksheetMapper worksheetMapper;
    @Autowired
    private DocumentMapper documentMapper;
    @Override
    public R addRepairOrder(RepairOrder repairOrder) {
            R r=new R();
            int result=repairOrderMapper.addRepairOrder(repairOrder);
            r.setCode(200);
            r.setMsg("添加成功");
            r.setData(repairOrder);
        return r;
    }

    public R updateRepairOrder(RepairOrder repairOrder){
        R r=new R();
        int result=repairOrderMapper.updateRepairOrder(repairOrder);
        r.setCode(200);
        r.setMsg("修改成功");
        return r;
    }
//查询接口
    @Override
    public JsonResult queryRepairOrder(PageRecv pageRecv) {
        JsonResult jsonResult=new JsonResult();
        Integer param1=(pageRecv.getCurrentPage()-1)* pageRecv.getPageSize();
        pageRecv.setCurrentPage(param1);
        List<RepairOrder> repairOrderList= repairOrderMapper.queryRepairOrder(pageRecv);
        Integer count =repairOrderMapper.queryCountRepairOrder(pageRecv);
        for(RepairOrder repairOrder:repairOrderList){
            Integer workSheetNumber= worksheetMapper.CountByRepair((int) repairOrder.getRepair_id());
            Integer DocumentNumber= documentMapper.CountByRepair((int) repairOrder.getRepair_id());
            repairOrder.setDocumentNumber(DocumentNumber);
            repairOrder.setWorkSheetNumber(workSheetNumber);
        }
        jsonResult.setCode(200);
        jsonResult.setData(repairOrderList);
        jsonResult.setMsg("查询成功");
        jsonResult.setCount(count);
        return jsonResult;
    }
//高级搜索
    @Override
    public JsonResult selectRepairOrder(PageRecv pageRecv) {
        JsonResult jsonResult=new JsonResult();
        Integer param1=(pageRecv.getCurrentPage()-1)* pageRecv.getPageSize();
        pageRecv.setCurrentPage(param1);
        List<RepairOrder> repairOrderList=repairOrderMapper.selectRepairOrder(pageRecv);
        Integer count= repairOrderMapper.selectCountRepairOrder(pageRecv);
        for(RepairOrder repairOrder:repairOrderList){
            Integer workSheetNumber= worksheetMapper.CountByRepair((int) repairOrder.getRepair_id());
            Integer DocumentNumber= documentMapper.CountByRepair((int) repairOrder.getRepair_id());
            repairOrder.setDocumentNumber(DocumentNumber);
            repairOrder.setWorkSheetNumber(workSheetNumber);
        }
        jsonResult.setCode(200);
        jsonResult.setMsg("搜索成功");
        jsonResult.setData(repairOrderList);
        jsonResult.setCount(count);
        return jsonResult;
    }
//搜索
    @Override
    public JsonResult selectRepairOrderByOther(PageRecv pageRecv) {
        JsonResult jsonResult=new JsonResult();
        Integer param1=(pageRecv.getCurrentPage()-1)* pageRecv.getPageSize();
        pageRecv.setCurrentPage(param1);
        List<RepairOrder> repairOrderList= repairOrderMapper.selectRepairOrderByOther(pageRecv);
        Integer count= repairOrderMapper.selectCountRepairOrderByOther(pageRecv);
        for(RepairOrder repairOrder:repairOrderList){
            Integer workSheetNumber= worksheetMapper.CountByRepair((int) repairOrder.getRepair_id());
            Integer DocumentNumber= documentMapper.CountByRepair((int) repairOrder.getRepair_id());
            repairOrder.setDocumentNumber(DocumentNumber);
            repairOrder.setWorkSheetNumber(workSheetNumber);
        }
        jsonResult.setCount(count);
        jsonResult.setData(repairOrderList);
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        return jsonResult;
    }
//删除
    @Override
    public R deleteRepairOrder(Integer repair_id) {
        R r=new R();
        repairOrderMapper.deleteRepairOrder(repair_id);
        r.setCode(200);
        r.setMsg("删除成功");
        return r;
    }
    //数据统计按员工
    @Override
    public R dataStatisticalByEmployee(String start_date,String stop_date) {
//        先获得员工姓名，推荐使用map集合再查看该员工发起的工单数量
        R r=new R();
        Map<String, Integer> employeemap=new HashMap<>();
        List<Employee> employeeList=employeeMapper.dataStatistics();
        for(int i=0;i<employeeList.size();i++){
            Integer count= repairOrderMapper.dataStatisticsCount(employeeList.get(i).getEmployee_name(),start_date,stop_date);
            employeemap.put(employeeList.get(i).getEmployee_name(),count);
        }
        r.setCode(200);
        r.setMsg("员工数据统计");
        r.setData(employeemap);
        return r;
    }
    //数据统计按部门
    @Override
    public R dataStatisticalByDept(String start_date,String stop_date) {
//        先获取员工姓名和所属部门，确定该员工发起的工单数量推荐使用set集合和map集合
//        先查出各个员工的发起工单数量
        //再查询出所有的部门，把员工的工单数量存到对应的部门中
        R r=new R();
        Set<String> deptSet=new HashSet<>();
        Map<String,Integer>deptMap=new HashMap<>();
        List<Employee> employeeList=employeeMapper.dataStatistics();
        for (int i=0;i<employeeList.size();i++){
            deptSet.add(employeeList.get(i).getDept().getDept_name());
        }
        int sum=0;
        for (String deptname:deptSet){
            for(int i=0;i<employeeList.size();i++){

                if(deptname.equals(employeeList.get(i).getDept().getDept_name())) {
                    Integer count = repairOrderMapper.dataStatisticsCount(employeeList.get(i).getEmployee_name(),start_date,stop_date);
                    sum=sum+count;
                }
            }
            deptMap.put(deptname,sum);
            sum=0;
        }
        r.setData(deptMap);
        r.setCode(200);
        r.setMsg("成功");
        return r;
    }

    //客户模块彻底删除
    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deleteAllByRepairOrder(Integer customer_id) {
        //查询属于该客户的工单
       List<Integer> repairids= repairOrderMapper.selectRepairByCustomerId(customer_id);

        //工单不为空就删除工单及其所有信息
        if (repairids!=null){
            //删除对应的工作记录
            worksheetMapper.deleteWorkSheetByRepairId(repairids);
            //删除对应的附件

        }
        //删除客户的对应工单
        repairOrderMapper.deleteByCustomerId(customer_id);
        documentMapper.deleteByRepair(repairids);
        return null;
    }


}
